package poker;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.Timer;

import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


//import HoofdSchermGUI.HostGameListener;

public class ConnectGUI extends JFrame implements ActionListener
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	Speler s;
	
	
	JButton b1,b2, b3;
	JPanel p1, p2;
	JTextField name, port, port2, ip;
	JFrame so, hostG;
	private HoofdSchermGUI h1;
	private Client client;
	public ConnectGUI(Speler s)
	{	
		this.s=s;
		this.setTitle("" + this.s.getNaam() + " choose a connectiontype");
		
		
		b1 = new JButton("Join");
		b3 = new JButton("Host new game");
		b2 = new JButton("Cancel");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		p1 = new JPanel(); p2 = new JPanel();
		
		getContentPane().add(p1, BorderLayout.NORTH);
		p2.add(b3);
		p2.add(b1); p2.add(b2);
		getContentPane().add(p2, BorderLayout.SOUTH);
		
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		this.setLocation( x, y );
		this.setSize(400,100);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);	
		this.setVisible(true);
		
		
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == b2)
		{
			System.exit(0);
		}
		if(e.getSource() == b1)
		{	
			/*
			h1 = new HoofdSchermGUI();
			this.dispose();
			*/
			this.showJoinGameMenu();
			this.dispose();
		}
		if(e.getSource() == b3)
		{
			this.showHostGameMenu();
			this.dispose();
		}
	}
	
	JFrame joinG;
	public void showJoinGameMenu()
	{
		joinG = new JFrame();
		Container c = joinG.getContentPane();
		GridBagConstraints cc = new GridBagConstraints();
		cc.fill = GridBagConstraints.HORIZONTAL;
		cc.insets = new Insets(10,0,0,10);
		JPanel jg = new JPanel();
		jg.setLayout(new GridBagLayout());
		
		cc.gridx = 0;
		cc.gridy = 0;
		
		jg.add( new JLabel("Enter IP-Address"), cc );
		ip = new JTextField("127.0.0.1",15);
		
		cc.gridx = 1;
		
		
		jg.add(ip, cc);
		
		cc.gridx = 0;
		cc.gridy = 1;
		
		jg.add(new JLabel("Enter Port"), cc);
		
		cc.gridx = 1;
		
		String portnr = "";
		try {
			Properties p = new Properties();
			p.load( new FileInputStream("settings.ini") );
			portnr = p.getProperty("Port");
		}
		catch(IOException exception) {}
		port2 = new JTextField(portnr, 15);
		jg.add(port2, cc);
		
		JButton join = new JButton("Join");
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if(e.getActionCommand().equals("Cancel"))
				{
					joinG.dispose();
					new ConnectGUI(s);
				}
			}
		});
		cc.gridx = 0;
		cc.gridy=2;
		jg.add(join, cc);
		cc.gridx = 1;
		cc.gridy = 2;
		jg.add(cancel, cc);
		join.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if(!(port2.getText().matches("\\d{1,5}")))
				{
					JOptionPane.showMessageDialog(null, "you did not specify the port correctly");
				}
				else
				{
					if(ConnectGUI.this.ip.getText().matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"))
					{
						//System.out.println("valid ip...connecting?...."); /* CONNECT - ben niet zo gek om dit ook nog te maken ;) */
						
						client = new Client(s, ConnectGUI.this.ip.getText(), Integer.parseInt(port2.getText()));
						
						client.start();
						//client.interrupt();
						
						while(client.spel == null)
						{
							try {
								Thread.sleep(1000);
								//System.out.println("ConnectGUI: client heeft nog steeds geen spel");
							} catch (InterruptedException e1) {}
						}
						HoofdSchermGUI hs = new HoofdSchermGUI(client);
						
						
						joinG.dispose();
						ConnectGUI.this.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "This is not a valid ip");
					}
				}
			}
		});
		
		c.add(jg);
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		
		joinG.setLocation( x, y );
		
		joinG.setSize(250, 200);
		joinG.setResizable(false);
		joinG.setTitle("Join a game");
		joinG.setVisible(true);
		joinG.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		joinG.validate();
	}
	
	private JButton cancel;
	
	public void showHostGameMenu()
	{
		hostG = new JFrame();
		Container c = hostG.getContentPane();
		
		JPanel hg = new JPanel();
		hg.setLayout(new GridLayout(7,1));
		
		JPanel t;
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);

		String portnr = "";
		String spelNaam = "";
		try {
			Properties p = new Properties();
			p.load( new FileInputStream("settings.ini") );
			portnr = p.getProperty("Port");
			spelNaam = p.getProperty("ServerName");
		}
		catch(IOException exception) {}
				
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		t.add( new JLabel("Game name") );
		t.add( Box.createRigidArea( new Dimension(10, 1) ) );
		name = new JTextField(spelNaam, 30);
		t.add(name);
		t.add( Box.createRigidArea( new Dimension(10, 1) ) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		t.add( new JLabel("Port") );
		t.add( Box.createRigidArea( new Dimension(45, 1) ) );
		port = new JTextField(portnr, 5);
		t.add(port);
		t.add( Box.createRigidArea( new Dimension(60, 1) ) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		t.setPreferredSize( new Dimension(1, 5) );
		
		hg.add(t);
		
		t = new JPanel();
		t.setLayout(new BoxLayout(t, BoxLayout.X_AXIS));
		
		HostGameListener hgl = new HostGameListener(hostG, this.s);
		t.add( Box.createRigidArea( new Dimension(20, 1) ) );
		
		JButton host = new JButton("Host");
		t.add(host);
		host.addActionListener(hgl);
		
		cancel = new JButton("Cancel");
		t.add(cancel);
		cancel.addActionListener(hgl);
		
		hg.add(t);
		
		c.add(hg);
		java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ( (ss.getWidth() / 2 ) - 150 );
		int y = (int) ( (ss.getHeight() / 2 ) - 150 );
		
		hostG.setLocation( x, y );
		
		hostG.setSize(200, 170);
		hostG.setResizable(false);
		hostG.setTitle("Host a game");
		hostG.setVisible(true);
		hostG.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		hostG.validate();
	}
	
	private JButton cancel1;
	private Timer timer;
	public class HostGameListener implements ActionListener 
	{
		private JFrame hg;
		Speler s;
		Server server;
		JButton start;
		private JList spelerList = new JList();
		private Client client;
		
		public HostGameListener(JFrame hg, Speler ss)
		{
			this.s =ss;
			this.hg = hg;
			
			spelerList.setPreferredSize(new Dimension(280,380));
		}
		
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == timer)
			{
				Iterator<ServerConnection> i = server.getClients().iterator();
				Speler s;
				ServerConnection c;
				Vector<String> v = new Vector<String>();
				for(int d = 0; d < server.getClients().size(); d++)
				{
					c = server.getClients().get(d);
					s = c.speler;
					v.add(s.getNaam() + " \t " + c.ping);
				}
				spelerList.setListData(v);
				start.setEnabled(v.size() >= 2);
				hg.validate();
			} else
			if(e.getActionCommand() == "Host")
			{
				if(!(name.getText().matches("[a-zA-Z \\-0-9.]+")))
				{	
					JOptionPane.showMessageDialog(null, "You did not enter a name!");
				} else 
				if(!(port.getText().matches("\\d{1,5}")))
				{	
					JOptionPane.showMessageDialog(null, "you did not specify the port");
				}
				else
				{	
					try
					{
						hostG.dispose();
						int portnr = Integer.parseInt(port.getText());
						KaartStapel stapel = new KaartStapel();
						//System.out.println(stapel);
						Spel spel = new Spel( stapel );
						server = new Server(spel, portnr);
						server.start();
						client = new Client(s, "localhost", portnr);
						client.start();
						timer = new Timer(10, this);
						timer.start();
						java.awt.Dimension ss = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
						int x = (int) ( (ss.getWidth() / 2 ) - 150 );
						int y = (int) ( (ss.getHeight() / 2 ) - 150 );
						JPanel p = new JPanel();
						JPanel pp = new JPanel();
						//System.out.println("Host Game");
						so = new JFrame();
						so.setLocation( x, y );
						so.setTitle("Overzicht Spelers");
						so.setSize(300, 400);
						start = new JButton("start game");
						
						start.setEnabled(false);
						
						cancel1 = new JButton("Cancel");
						start.addActionListener(this);
						cancel1.addActionListener(this);
						pp.add(start); pp.add(cancel1);
						start.setEnabled(true);
						Container c = so.getContentPane();
						p.add(spelerList);
						c.add(p, BorderLayout.CENTER);
						c.add(pp, BorderLayout.SOUTH);
						so.setVisible(true);
						so.setDefaultCloseOperation(so.EXIT_ON_CLOSE);
					}
					catch(NumberFormatException ne)
					{
						JOptionPane.showMessageDialog(null, "This is not a valid port");
					}
				}
			}
			else
			if(e.getSource() == cancel)
			{
				hostG.dispose();
				new ConnectGUI(s);
			}
			else if(e.getSource() == cancel1)
			{
				timer.stop();
				server.stop();
				client.stop();
				so.dispose();
				showHostGameMenu();
			}
			else
			if(e.getActionCommand().equals("start game"))
			{
				server.startGame();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {	}
				server.syncAll();
				h1 = new HoofdSchermGUI(client);
				//client.setHoofdScherm(h1);
				so.dispose();
				ConnectGUI.this.dispose();
			}
			
		}
		
	};
	
	
}
